package com.rsxy.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rsxy.service.pojo.RsComments;
import com.rsxy.service.service.RsCommentService;
import com.rsxy.service.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/comment")
@Slf4j
@Api(tags= "评论相关接口")
public class CommentController {

    @Autowired
    private RsCommentService rsCommentService;
    @ApiOperation(value = "添加评论",notes = "测试数据")
    @ApiImplicitParams(
            {       @ApiImplicitParam(name="productId",defaultValue = "0",required = true,paramType = "body",dataType = "string"),
                    @ApiImplicitParam(name="userId",defaultValue = "0",required = true,paramType = "body",dataType = "string"),
                    @ApiImplicitParam(name="commentContent",defaultValue = "0",required = true,paramType = "body",dataType = "string"),
                    @ApiImplicitParam(name="commentFrom",defaultValue = "0",required = true,paramType = "body",dataType = "string"),
                    @ApiImplicitParam(name="nickName",defaultValue = "0",required = true,paramType = "body",dataType = "string"),
                    @ApiImplicitParam(name="avatarUrl",defaultValue = "0",required = true,paramType = "body",dataType = "string"),
                    @ApiImplicitParam(name="commentType",defaultValue = "0",required = true,paramType = "body",dataType = "integer"),
                    @ApiImplicitParam(name="id",defaultValue = "0",required = false,paramType = "body",dataType = "integer"),
                    @ApiImplicitParam(name="gmtCreate",defaultValue = "0",required = false,paramType = "body",dataType = "data"),
            })
    @RequestMapping(method = RequestMethod.POST,value = "/addComment")
    public String addComment(RsComments rsComments){
        int insert = rsCommentService.getBaseMapper().insert(rsComments);
        if (insert>0){
            return ResultUtil.success(200,"发表成功",null);
        }else {
            return ResultUtil.erro(200,"发表失败",null);
        }
    }
    @RequestMapping(method = RequestMethod.GET,value = "/getPageComment")
    public IPage<RsComments> getPageList(Integer page, Integer limit){
        Page<RsComments> page2=new Page<>(page,limit);
        IPage<RsComments> page1 = rsCommentService.getBaseMapper().selectPage(page2, null);
        return page1;
    }

    @RequestMapping(method = RequestMethod.DELETE,value = "/{commentId}")
    public String deleteById(@PathVariable("commentId") String commentId){
        QueryWrapper<RsComments> rsProductQueryWrapper=new QueryWrapper<>();
        rsProductQueryWrapper.eq("comment_id",commentId);
        int delete = rsCommentService.getBaseMapper().delete(rsProductQueryWrapper);
        if (delete>0){
            return ResultUtil.success(200,"删除成功",null);
        }
        return ResultUtil.success(200,"删除失败",null);
    }
}
